package com.example.novelweb.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.novelweb.common.BaseResponse;
import com.example.novelweb.common.ErrorCode;
import com.example.novelweb.common.ResultUtils;
import com.example.novelweb.exception.BusinessException;
import com.example.novelweb.exception.ThrowUtils;
import com.example.novelweb.model.dto.novel.NovelAddRequest;
import com.example.novelweb.model.dto.novel.NovelQueryRequest;
import com.example.novelweb.model.dto.novel.NovelUpdateRequest;
import com.example.novelweb.model.vo.NovelInfo;
import com.example.novelweb.model.vo.NovelVO;
import com.example.novelweb.service.NovelService;
import io.swagger.annotations.ApiImplicitParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/novel")
public class NovelController {
  @Resource
  NovelService novelService;

  @GetMapping("/page")
  public BaseResponse<Page<NovelVO>> PageAllNovel(NovelQueryRequest novelQueryRequest, HttpServletRequest httpServletRequest) {
    ThrowUtils.throwIf(novelQueryRequest == null,ErrorCode.PARAMS_ERROR,"null params");
    return novelService.pageAll(novelQueryRequest);
  }

  @GetMapping("/page/search")
  public BaseResponse<Page<NovelVO>> pageNovelBySearch(NovelQueryRequest novelQueryRequest,HttpServletRequest httpServletRequest) {
    ThrowUtils.throwIf(novelQueryRequest == null,ErrorCode.PARAMS_ERROR,"null params");
    // assert notNull
//TODO: search by isEnd
    if(StringUtils.isAllBlank(
        novelQueryRequest.getName()
        ,novelQueryRequest.getUserName()
        ,novelQueryRequest.getTags()
    )) {
      throw new BusinessException(ErrorCode.PARAMS_ERROR,"null params");
    }
    return novelService.pageBySearch(novelQueryRequest);
  }


  @ApiImplicitParam(name = "Authorization", value = "Authorization",required = false, dataType = "String",paramType="header")
  @GetMapping("/info")
  public BaseResponse<NovelInfo> getNovelInfo(Long id, HttpServletRequest httpServletRequest) {
    ThrowUtils.throwIf(id == null || id<=0,ErrorCode.PARAMS_ERROR,"null or invalid param id");

    return novelService.getInfo(id);

  }

  /**
   * @return NovelInfo ignore (more,other infos)
   * */

  @GetMapping("/info-simple")
  public BaseResponse<NovelVO> getNovelSimpleInfo(Long id, HttpServletRequest httpServletRequest) {
    ThrowUtils.throwIf(id == null || id<=0,ErrorCode.PARAMS_ERROR,"null or invalid param id");

    return ResultUtils.success(novelService.getSimpleInfo(id));

  }

  /**
   * @return ignore NovelVO.tags , .User user , description
   * */
  @GetMapping("/listByUser")
  public BaseResponse<List<NovelVO>> listByUserId(Long userId,HttpServletRequest httpServletRequest) {
    ThrowUtils.throwIf(userId == null || userId<=0,ErrorCode.PARAMS_ERROR,"null or invalid param id");
    return novelService.listByUserId(userId);
  }

  @ApiImplicitParam(name = "Authorization", value = "Authorization",required = true, dataType = "String",paramType="header")
  @PostMapping("/add")
  public BaseResponse<Boolean> addNovel(@RequestBody NovelAddRequest novelAddRequest, HttpServletRequest httpServletRequest) {
    if(StringUtils.isAnyBlank(
        novelAddRequest.getName()
        ,novelAddRequest.getDescription()
        ,novelAddRequest.getTags()
    )) {
      throw new BusinessException(ErrorCode.PARAMS_ERROR,"存在空参数");
    }
    return novelService.add(novelAddRequest);
  }

  @ApiImplicitParam(name = "Authorization", value = "Authorization",required = true, dataType = "String",paramType="header")
  @PostMapping("/update")
  public BaseResponse<Boolean> updateNovel(@RequestBody NovelUpdateRequest novelUpdateRequest, HttpServletRequest httpServletRequest) {
    Long id = novelUpdateRequest.getId();
    ThrowUtils.throwIf(id==null || id<=0, ErrorCode.PARAMS_ERROR,"null or invalid id");
    if(StringUtils.isAllBlank(
        novelUpdateRequest.getName()
        ,novelUpdateRequest.getDescription()
        ,novelUpdateRequest.getCover()
    )) {
      throw new BusinessException(ErrorCode.PARAMS_ERROR,"全空参数");
    }
    return novelService.updateNovel(novelUpdateRequest);
  }

  @ApiImplicitParam(name = "Authorization", value = "Authorization",required = true, dataType = "String",paramType="header")
  @PostMapping("/del")
  public BaseResponse<Boolean> delNovel(@RequestParam("id") Long id, HttpServletRequest httpServletRequest) {
    ThrowUtils.throwIf(id==null || id<=0, ErrorCode.PARAMS_ERROR,"null or invalid id");

    return novelService.delNovel(id);
  }




}
